package org.example;

import org.UserRepository;
import org.example.controller.UserController;
import org.example.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class 核心容器 {
    public static void main(String[] args) {
        //第一步：创建一个核心容器:基于包扫描注解的方式创建容器
        //扫描org.example包，下边的子包也会被扫描到
        ApplicationContext context = new AnnotationConfigApplicationContext("org.example");//可以无参，也可以传入一个参数（可变参数）

        //这里像是hashmap，存储beadID和bean对象,这里的对象可以是任意类型，所以要强制转换
        //获取Bean对象，使用容器对象.getBean(bean的id/名称)
        UserService userService = (UserService) context.getBean("userService");
        System.out.println("userService: " + userService);
        //没有被扫描到的包不会注册bean对象到容器中
//        UserRepository userRepository = (UserRepository) context.getBean("userRepository");
//        System.out.println(userRepository);

        String zhangsan = (String)context.getBean("zhangsan");
        System.out.println(zhangsan);
        String zhangsan2 = (String)context.getBean("zhangsan");
        System.out.println(zhangsan2);

        UserService us1 = (UserService) context.getBean("us1");
        System.out.println("us1: " + us1);

        UserService us2 = (UserService) context.getBean("us2");
        System.out.println("us2: " + us2);

        //获取UserController以及属性zhangsan
        System.out.println("########################### ");
        UserController userController = (UserController) context.getBean("userController");
        System.out.printf("userController: %s,zhangsan : %s \n",userController,userController.getZhangsan() );
        UserController userController2 = (UserController) context.getBean("userController");
        System.out.printf("userController2: %s,zhangsan : %s \n",userController2,userController2.getZhangsan2() );
        System.out.println(userController.getUserService());
        System.out.println("########################### ");


        System.out.println(userController.getUs1());

        System.out.println(userController.getUshahah());
    }
}
